package com.kyh.interceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.kyh.sys.entity.Admin;
import com.kyh.sys.mapper.AdminMapper;
import com.kyh.util.ReqUtil;

/**
 * 判断登录拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {
	
    @Autowired
    private AdminMapper adminMapper;

    /**
     * 开始进入地址请求拦截
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
    	
       /* Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                if (cookies[i].getName().equals(ReqUtil.ADMIN_TOKEN)) {
                    String value = cookies[i].getValue();
                    if (value != null && value.length() > 0) {
                        Long adminid = ReqUtil.getAdminId(request);
                        Admin admin = adminMapper.get(adminid);
                        if (admin != null) {
                            if (value.indexOf(admin.getAdmintoken()) > -1 && admin.getStatus().intValue() == 1) {
                                return true;
                            }
                        }
                    }
                }
            }
        }
        response.sendRedirect("/login");    //未登录，跳转到登录页
        return false;*/
        return true;
    }

    /**
     * 处理请求完成后视图渲染之前的处理操作
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {

    }

    /**
     * 视图渲染之后的操作
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {

    }

}
